package v3;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import util.FileUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Properties;

public class NetConfigUtil {
    private static String path="/cytech/app/sysconfig/{sn}/netConfig.json";
    public static String sn;

    static {
        init();
    }

    public static void init() {
        try {
            //先获取sn
            sn = FileUtil.readString("/cytech/app/sn.txt", "utf-8");
            sn = sn.replaceAll("'","");
            sn = sn.replaceAll("\\s+","");
            path = path.replace("{sn}", sn);
            //如果文件不存在就创建
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }



    }

    public static JSONObject get() {
        String content = FileUtil.readString(path, "utf-8");
        if (content == null && JSONUtil.isJson(content)) {
            return JSONUtil.parseObj(content);
        } else {
            return  new JSONObject();
        }
    }

    public static void set(JSONObject json) {
        FileUtil.writeString(json.toString(), new File(path), Charset.forName("UTF-8"));
    }

    public static void main(String[] args) {
        get();
    }
}
